import { assingRoleSaveApi, getRoleIdApi } from '@/api/system/user/user'
import { RoleListParam } from '@/api/system/role/RoleModel'
import { SelectRoleParam } from '@/api/system/user/UserModel'
import useInstance from '@/hooks/useInstance'
import { StatusCode } from '@/http/request'
import { DialogModel } from '@/types/BaseType'
import { nextTick, onMounted, reactive, ref } from 'vue'
import { getRoleListApi } from '@/api/system/role/role'

export default function useAssignRole(
    dialogModel: DialogModel,
    onShow: any,
    onClose: any
) {
    const { global } = useInstance()

    // 表格高度
    const tableHeight = ref(0)

    // 被分配角色的用户id
    const selectUserId = ref<number>(0)

    // 选中的角色id
    const selectRoleId = ref(0)

    const onConfirm = async () => {
        if (!selectRoleId.value) {
            global.$message({ message: '请选择角色', type: 'warning' })
            return false
        }
        let param = reactive<SelectRoleParam>({
            role_id: selectRoleId.value,
            user_id: selectUserId.value
        })
        console.log('分配：', param)
        let res = await assingRoleSaveApi(param)
        if (res && res.code == StatusCode.Success) {
            global.$message({ message: res.msg, type: 'success' })
            onClose()
        } else {
            global.$message({ message: res.msg, type: 'error' })
        }
    }
    const show = async (name: string, userId: number) => {
        selectUserId.value = userId
        selectRoleId.value = 0
        // 查询该用户原来的角色id
        let res = await getRoleIdApi(userId)
        if (res && res.code == StatusCode.Success) {
            if (res.data) {
                selectRoleId.value = res.data.role_id
            }
        }
        console.log('原来的角色id：', selectRoleId.value)
        // 设置弹框标题
        dialogModel.title = '为【' + name + '】分配角色'
        // 显示弹框
        onShow()
    }

    // 角色列表查询参数
    const roleParams = reactive<RoleListParam>({
        current_page: 1,
        page_size: 8,
        role_name: '',
        status: '1',
        total: 0
    })

    // 角色列表数据
    const roleList = reactive({
        list: []
    })

    // 获取列表数据
    const getRoleList = async () => {
        let res = await getRoleListApi(roleParams)
        if (res && res.code == StatusCode.Success) {
            roleList.list = res.data.list
            roleParams.total = res.data.total
        }
    }

    const sizeChange = (size: number) => {
        roleParams.page_size = size
        getRoleList()
    }

    const currentChange = (page: number) => {
        roleParams.current_page = page
        getRoleList()
    }

    const getSelectRole = (id: number) => {
        console.log('选中的角色id：', id)
        selectRoleId.value = id
    }

    const cellClickHandle = (row: any) => {
        getSelectRole(row.id)
    }

    onMounted(() => {
        nextTick(() => {
            tableHeight.value = window.innerHeight - 490
        })
        getRoleList()
    })

    return {
        onConfirm,
        show,
        roleParams,
        roleList,
        sizeChange,
        currentChange,
        tableHeight,
        selectRoleId,
        getSelectRole,
        cellClickHandle
    }
}
